package cn.seocoo.platform.api.controller.car;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.base.domain.BaseVehicleType;
import cn.seocoo.platform.base.service.IBaseVehicleTypeService;
import cn.seocoo.platform.car.domain.CarInfo;
import cn.seocoo.platform.car.dto.CarFrom;
import cn.seocoo.platform.car.service.ICarInfoService;
import cn.seocoo.platform.common.constant.ClassContants;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.user.domain.AppUser;
import cn.seocoo.platform.user.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: ApiPublishCarController
 * @description: 发布车辆
 * @author: wujian
 * @Date: 2019-12-30 23:20
 **/
@RestController
@RequestMapping("/api/car")
public class ApiPublishCarController {
    @Autowired
    private IBaseVehicleTypeService iBaseVehicleTypeService;
    @Autowired
    private IAppUserService iAppUserService;
    @Autowired
    private ICarInfoService iCarInfoService;


    /**
     * @return cn.seocoo.platform.common.core.domain.AjaxResult
     * @Author WuJian
     * @Description //发布车辆【注意】挂车没有排放标准、发动机、马力、燃料、行驶公里、保险有效期；
     * @Date 23:22 2019/12/30
     * @Param []
     **/
    @PostMapping("/publishCar")
    public JSONResult publishCar(@RequestBody CarFrom carFrom) {
        try {
            if (carFrom.getAppUserId() == null) {
                return JSONResult.errorMsg("用户不能为空");
            }
            AppUser appUser = iAppUserService.selectAppUserById(carFrom.getAppUserId());
            if (appUser != null) {
                //判断当前用户发布多少辆车
                int count = iCarInfoService.getCountCarWithAppUserId(appUser.getId());
                if (count > 0) {
                    if (!AppUser.BUSINESSES_USER.equals(appUser.getUserType())) {
                        return JSONResult.errorMsg(CarInfo.MSG);
                    }
                }
            }
            if (carFrom.getBaseVehicleId() == null) {
                return JSONResult.errorMsg("请选择车型");
            }
            if (carFrom.getBaseCategoryId() == null) {
                return JSONResult.errorMsg("请选择车型类别");
            }
            if (StringUtils.isBlank(carFrom.getPicturePath())) {
                return JSONResult.errorMsg("请上传图片");
            }
            if (carFrom.getBaseDrivingTypeId() == null) {
                return JSONResult.errorMsg("请选择驱动/轴数形式");
            }

            BaseVehicleType baseVehicleType = iBaseVehicleTypeService.selectBaseVehicleTypeById(carFrom.getBaseVehicleId());
            //如果是挂车,一些选项不是必填项
            if (!ClassContants.CAR_TYPE_NAME.equals(baseVehicleType.getVehicleName())) {
                //汽车品牌
                if (carFrom.getBaseBrandId() == null) {
                    return JSONResult.errorMsg("请选择车型品牌");
                }
                //排放标准
                if (carFrom.getBaseEmissionStandard() == null) {
                    return JSONResult.errorMsg("请选择排放标准");
                }
                //发动机
                if (carFrom.getBaseEngine() == null) {
                    return JSONResult.errorMsg("请选择发动机品牌");
                }
                //马力
                if (StringUtils.isBlank(carFrom.getBaseHorsepower())) {
                    return JSONResult.errorMsg("请填写马力");
                }
                //燃料
                if (carFrom.getBaseFuel() == null) {
                    return JSONResult.errorMsg("请选择排燃料");
                }
                //行驶公里
                if (StringUtils.isBlank(carFrom.getTravelling())) {
                    return JSONResult.errorMsg("请填写行驶公里");
                }
                //保险有效期
                if (carFrom.getInsuranceTime() == null) {
                    return JSONResult.errorMsg("请选择保险有效期");
                }
            }
            if (carFrom.getPlateHomeId() == null) {
                return JSONResult.errorMsg("请选择车户所在地");
            }
            if (carFrom.getBaseTransferType() == null) {
                return JSONResult.errorMsg("请选择过户类型");
            }
            if (carFrom.getBasePaymentType() == null) {
                return JSONResult.errorMsg("请选择付款方式");
            }
            if (StringUtils.isBlank(carFrom.getIntentionAmount())) {
                return JSONResult.errorMsg("请填写意向价格");
            }
            if (StringUtils.isBlank(carFrom.getContacts())) {
                return JSONResult.errorMsg("请填写联系人");
            }
            if (StringUtils.isBlank(carFrom.getContactsNumber())) {
                return JSONResult.errorMsg("请填写联系电话");
            }
            if (StringUtils.isBlank(carFrom.getTradingArea())) {
                return JSONResult.errorMsg("请选择交易地区");
            }
            Map<String,String> map = iCarInfoService.publishCar(carFrom);

            return JSONResult.ok("发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }

    }
}
